





"""simple docstring"""





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


_a							=							logging.get_logger(__name__)

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




class  _lowerCAmelCase  (     _a       ):







 """simple docstring"""







 __UpperCAmelCase     : str       		=     """instructblip_vision_model"""


 def __init__(      self							:      Union[str, Any], UpperCAmelCase__							:      str=1_4_0_8, UpperCAmelCase__							:      str=6_1_4_4, UpperCAmelCase__							:      List[Any]=3_9, UpperCAmelCase__							:      str=1_6, UpperCAmelCase__							:      Optional[int]=2_2_4, UpperCAmelCase__							:      Dict=1_4, UpperCAmelCase__							:      str="gelu", UpperCAmelCase__							:      int=1E-6, UpperCAmelCase__							:      Union[str, Any]=0.0, UpperCAmelCase__							:      Tuple=1E-10, UpperCAmelCase__							:      List[str]=True, **UpperCAmelCase__							:      str, ):
    super().__init__(**__lowerCamelCase     )

    __lowercase								=						hidden_size
    __lowercase								=						intermediate_size
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						patch_size
    __lowercase								=						image_size
    __lowercase								=						initializer_range
    __lowercase								=						attention_dropout
    __lowercase								=						layer_norm_eps
    __lowercase								=						hidden_act
    __lowercase								=						qkv_bias



 @classmethod
 def  _lowercase			(      cls							:      Union[str, Any], UpperCAmelCase__							:      Union[str, os.PathLike], **UpperCAmelCase__							:      List[str]     ):
    cls._set_token_in_kwargs(__lowerCamelCase     )

    __lowercase								=						cls.get_config_dict(__lowerCamelCase, **__lowerCamelCase     )

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







 """simple docstring"""







 __UpperCAmelCase     : str       		=     """instructblip_qformer"""


 def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      Optional[int]=3_0_5_2_2, UpperCAmelCase__							:      Dict=7_6_8, UpperCAmelCase__							:      int=1_2, UpperCAmelCase__							:      str=1_2, UpperCAmelCase__							:      Optional[Any]=3_0_7_2, UpperCAmelCase__							:      List[Any]="gelu", UpperCAmelCase__							:      Optional[int]=0.1, UpperCAmelCase__							:      Any=0.1, UpperCAmelCase__							:      Union[str, Any]=5_1_2, UpperCAmelCase__							:      List[Any]=0.02, UpperCAmelCase__							:      Optional[int]=1E-12, UpperCAmelCase__							:      Tuple=0, UpperCAmelCase__							:      int="absolute", UpperCAmelCase__							:      str=2, UpperCAmelCase__							:      str=1_4_0_8, **UpperCAmelCase__							:      Tuple, ):
    super().__init__(pad_token_id=__lowerCamelCase, **__lowerCamelCase     )

    __lowercase								=						vocab_size
    __lowercase								=						hidden_size
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						hidden_act
    __lowercase								=						intermediate_size
    __lowercase								=						hidden_dropout_prob
    __lowercase								=						attention_probs_dropout_prob
    __lowercase								=						max_position_embeddings
    __lowercase								=						initializer_range
    __lowercase								=						layer_norm_eps
    __lowercase								=						position_embedding_type
    __lowercase								=						cross_attention_frequency
    __lowercase								=						encoder_hidden_size



 @classmethod
 def  _lowercase			(      cls							:      Optional[int], UpperCAmelCase__							:      Union[str, os.PathLike], **UpperCAmelCase__							:      Any     ):
    cls._set_token_in_kwargs(__lowerCamelCase     )

    __lowercase								=						cls.get_config_dict(__lowerCamelCase, **__lowerCamelCase     )

    # get the qformer config dict if we are loading from InstructBlipConfig
    if config_dict.get("model_type"     ) == "instructblip":
       __lowercase								=						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  (     _a       ):







 """simple docstring"""







 __UpperCAmelCase     : Tuple       		=     """instructblip"""
 __UpperCAmelCase     : Optional[Any]       		=     True


 def __init__(      self							:      Any, UpperCAmelCase__							:      Optional[int]=None, UpperCAmelCase__							:      Optional[Any]=None, UpperCAmelCase__							:      List[str]=None, UpperCAmelCase__							:      Dict=3_2, **UpperCAmelCase__							:      int     ):
    super().__init__(**__lowerCamelCase     )

    if vision_config is None:
       __lowercase								=						{}
       logger.info("vision_config is None. initializing the InstructBlipVisionConfig with default values."     )

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

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

    __lowercase								=						InstructBlipVisionConfig(**__lowerCamelCase     )
    __lowercase								=						InstructBlipQFormerConfig(**__lowerCamelCase     )
    __lowercase								=						text_config["""model_type"""] if """model_type""" in text_config else """opt"""
    __lowercase								=						CONFIG_MAPPING[text_model_type](**__lowerCamelCase     )

    __lowercase								=						self.text_config.tie_word_embeddings
    __lowercase								=						self.text_config.is_encoder_decoder

    __lowercase								=						num_query_tokens
    __lowercase								=						self.vision_config.hidden_size
    __lowercase								=						self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
    __lowercase								=						1.0
    __lowercase								=						0.02


 @classmethod
 def  _lowercase			(      cls							:      Union[str, Any], UpperCAmelCase__							:      InstructBlipVisionConfig, UpperCAmelCase__							:      InstructBlipQFormerConfig, UpperCAmelCase__							:      PretrainedConfig, **UpperCAmelCase__							:      List[Any], ):
    return cls(
        vision_config=vision_config.to_dict(), qformer_config=qformer_config.to_dict(), text_config=text_config.to_dict(), **__lowerCamelCase, )



 def  _lowercase			(      self							:      Tuple     ):
    __lowercase								=						copy.deepcopy(self.__dict__     )
    __lowercase								=						self.vision_config.to_dict()
    __lowercase								=						self.qformer_config.to_dict()
    __lowercase								=						self.text_config.to_dict()
    __lowercase								=						self.__class__.model_type
    return output







"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TextClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
_a							=							{'LayoutLMv2Config', 'LayoutLMv3Config'}




@is_pipeline_test
class  _lowerCAmelCase  (     unittest.TestCase       ):







 """simple docstring"""







 __UpperCAmelCase     : str       		=     MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
 __UpperCAmelCase     : str       		=     TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

 if model_mapping is not None:
  __UpperCAmelCase     : Optional[Any]       		=     {config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
 if tf_model_mapping is not None:
  __UpperCAmelCase     : Tuple       		=     {
      config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
  }


 @require_torch
 def  _lowercase			(      self							:      Optional[int]     ):
    __lowercase								=						pipeline(
        task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    __lowercase								=						text_classifier("This is great !", top_k=2     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]     )

    __lowercase								=						text_classifier(["This is great !", "This is bad"], top_k=2     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
        ], )

    __lowercase								=						text_classifier("This is great !", top_k=1     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    # Legacy behavior
    __lowercase								=						text_classifier("This is great !", return_all_scores=UpperCAmelCase__     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )

    __lowercase								=						text_classifier("This is great !", return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [[{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}]]     )

    __lowercase								=						text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
            [{"label": "LABEL_0", "score": 0.504}, {"label": "LABEL_1", "score": 0.496}],
        ], )

    __lowercase								=						text_classifier(["This is great !", "Something else"], return_all_scores=UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [
            {"label": "LABEL_0", "score": 0.504},
            {"label": "LABEL_0", "score": 0.504},
        ], )


 @require_torch
 def  _lowercase			(      self							:      Dict     ):
    import torch

    __lowercase								=						pipeline(
        task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt", device=torch.device("cpu"     ), )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )


 @require_tf
 def  _lowercase			(      self							:      Union[str, Any]     ):
    __lowercase								=						pipeline(
        task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="tf"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "LABEL_0", "score": 0.504}]     )


 @slow
 @require_torch
 def  _lowercase			(      self							:      Dict     ):
    __lowercase								=						pipeline("text-classification"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "POSITIVE", "score": 1.0}]     )
    __lowercase								=						text_classifier("This is bad !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "NEGATIVE", "score": 1.0}]     )
    __lowercase								=						text_classifier("Birds are a type of animal"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "POSITIVE", "score": 0.988}]     )


 @slow
 @require_tf
 def  _lowercase			(      self							:      Tuple     ):
    __lowercase								=						pipeline("text-classification", framework="tf"     )

    __lowercase								=						text_classifier("This is great !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "POSITIVE", "score": 1.0}]     )
    __lowercase								=						text_classifier("This is bad !"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "NEGATIVE", "score": 1.0}]     )
    __lowercase								=						text_classifier("Birds are a type of animal"     )
    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": "POSITIVE", "score": 0.988}]     )


 def  _lowercase			(      self							:      Union[str, Any], UpperCAmelCase__							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Tuple     ):
    __lowercase								=						TextClassificationPipeline(model=UpperCAmelCase__, tokenizer=UpperCAmelCase__     )
    return text_classifier, ["HuggingFace is in", "This is another test"]



 def  _lowercase			(      self							:      Any, UpperCAmelCase__							:      List[Any], UpperCAmelCase__							:      str     ):
    __lowercase								=						text_classifier.model
    # Small inputs because BartTokenizer tiny has maximum position embeddings = 22
    __lowercase								=						"HuggingFace is in"
    __lowercase								=						text_classifier(UpperCAmelCase__     )

    self.assertEqual(nested_simplify(UpperCAmelCase__     ), [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}]     )
    self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )

    __lowercase								=						["HuggingFace is in ", "Paris is in France"]
    __lowercase								=						text_classifier(UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}, {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}], )
    self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )
    self.assertTrue(outputs[1]["label"] in model.config.idalabel.values()     )

    # Forcing to get all results with `top_k=None`
    # This is NOT the legacy format
    __lowercase								=						text_classifier(UpperCAmelCase__, top_k=UpperCAmelCase__     )
    __lowercase								=						len(model.config.idalabel.values()     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [[{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N, [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}] * N], )

    __lowercase								=						{"text": "HuggingFace is in ", "text_pair": "Paris is in France"}
    __lowercase								=						text_classifier(UpperCAmelCase__     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), {"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}, )
    self.assertTrue(outputs["label"] in model.config.idalabel.values()     )

    # This might be used a text pair, but tokenizer + pipe interaction
    # makes it hard to understand that it's not using the pair properly
    # https://github.com/huggingface/transformers/issues/17305
    # We disabled this usage instead as it was outputting wrong outputs.
    __lowercase								=						[["HuggingFace is in ", "Paris is in France"]]
    with self.assertRaises(UpperCAmelCase__     ):
       text_classifier(UpperCAmelCase__     )

    # This used to be valid for doing text pairs
    # We're keeping it working because of backward compatibility
    __lowercase								=						text_classifier([[["HuggingFace is in ", "Paris is in France"]]]     )
    self.assertEqual(
        nested_simplify(UpperCAmelCase__     ), [{"label": ANY(UpperCAmelCase__     ), "score": ANY(UpperCAmelCase__     )}], )
    self.assertTrue(outputs[0]["label"] in model.config.idalabel.values()     )




