




"""simple docstring"""






import copy
import os
from typing import Union

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


lowercase__     						=						logging.get_logger(__name__)

lowercase__     						=						{
    """BAAI/AltCLIP""": """https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json""",
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}






class lowerCAmelCase__	(     lowercase						):






			'''simple docstring'''



			lowerCamelCase__      =			"""altclip_text_model"""
			def __init__(  self  ,      lowercase=250002  ,      lowercase=1024  ,      lowercase=24  ,      lowercase=16  ,      lowercase=4096  ,      lowercase="gelu"  ,      lowercase=0.1  ,      lowercase=0.1  ,      lowercase=514  ,      lowercase=1  ,      lowercase=0.02  ,      lowercase=0.02  ,      lowercase=1E-05  ,      lowercase=1  ,      lowercase=0  ,      lowercase=2  ,      lowercase="absolute"  ,      lowercase=True  ,      lowercase=768  ,      **lowercase  ,      ):
							super().__init__(pad_token_id=lowercase  ,      bos_token_id=lowercase  ,      eos_token_id=lowercase  ,      **lowercase  )

							_lowerCamelCase     :			Optional[Any]    =    vocab_size
							_lowerCamelCase     :			Optional[Any]    =    hidden_size
							_lowerCamelCase     :			Optional[int]    =    num_hidden_layers
							_lowerCamelCase     :			Union[str, Any]    =    num_attention_heads
							_lowerCamelCase     :			Union[str, Any]    =    hidden_act
							_lowerCamelCase     :			Tuple    =    intermediate_size
							_lowerCamelCase     :			str    =    hidden_dropout_prob
							_lowerCamelCase     :			Tuple    =    attention_probs_dropout_prob
							_lowerCamelCase     :			str    =    max_position_embeddings
							_lowerCamelCase     :			Union[str, Any]    =    type_vocab_size
							_lowerCamelCase     :			Optional[Any]    =    initializer_range
							_lowerCamelCase     :			List[str]    =    initializer_factor
							_lowerCamelCase     :			int    =    layer_norm_eps
							_lowerCamelCase     :			Dict    =    position_embedding_type
							_lowerCamelCase     :			Optional[Any]    =    use_cache
							_lowerCamelCase     :			List[Any]    =    project_dim






class lowerCAmelCase__	(     lowercase						):






			'''simple docstring'''



			lowerCamelCase__      =			"""altclip_vision_model"""
			def __init__(  self  ,      lowercase=768  ,      lowercase=3072  ,      lowercase=512  ,      lowercase=12  ,      lowercase=12  ,      lowercase=3  ,      lowercase=224  ,      lowercase=32  ,      lowercase="quick_gelu"  ,      lowercase=1E-5  ,      lowercase=0.0  ,      lowercase=0.02  ,      lowercase=1.0  ,      **lowercase  ,      ):
							super().__init__(**lowercase  )

							_lowerCamelCase     :			Optional[Any]    =    hidden_size
							_lowerCamelCase     :			Union[str, Any]    =    intermediate_size
							_lowerCamelCase     :			Tuple    =    projection_dim
							_lowerCamelCase     :			Any    =    num_hidden_layers
							_lowerCamelCase     :			List[Any]    =    num_attention_heads
							_lowerCamelCase     :			Union[str, Any]    =    num_channels
							_lowerCamelCase     :			Union[str, Any]    =    patch_size
							_lowerCamelCase     :			Optional[int]    =    image_size
							_lowerCamelCase     :			int    =    initializer_range
							_lowerCamelCase     :			Dict    =    initializer_factor
							_lowerCamelCase     :			Dict    =    attention_dropout
							_lowerCamelCase     :			Optional[int]    =    layer_norm_eps
							_lowerCamelCase     :			Union[str, Any]    =    hidden_act






			@classmethod
			def 					A_    (  cls  ,      lowercase  ,      **lowercase  ):
							cls._set_token_in_kwargs(lowercase  )

							_lowerCamelCase, _lowerCamelCase     :			Optional[Any]    =    cls.get_config_dict(lowercase  ,      **lowercase  )

							# get the vision config dict if we are loading from AltCLIPConfig
							if config_dict.get('model_type'  ) == "altclip":
											_lowerCamelCase     :			Optional[int]    =    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(lowercase  ,      **lowercase  )






class lowerCAmelCase__	(     lowercase						):






			'''simple docstring'''



			lowerCamelCase__      =			"""altclip"""
			lowerCamelCase__      =			True
			def __init__(  self  ,      lowercase=None  ,      lowercase=None  ,      lowercase=768  ,      lowercase=2.65_92  ,      **lowercase  ):
							# If `_config_dict` exist, we use them for the backward compatibility.
							# We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
							# of confusion!).
							_lowerCamelCase     :			Optional[int]    =    kwargs.pop('text_config_dict'  ,      lowercase  )
							_lowerCamelCase     :			Optional[int]    =    kwargs.pop('vision_config_dict'  ,      lowercase  )

							super().__init__(**lowercase  )

							# Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
							# `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
							# cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
							if text_config_dict is not None:
											if text_config is None:
															_lowerCamelCase     :			int    =    {}

											# This is the complete result when using `text_config_dict`.
											_lowerCamelCase     :			str    =    AltCLIPTextConfig(**lowercase  ).to_dict()

											# Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
											for key, value in _text_config_dict.items():
															if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
																			# If specified in `text_config_dict`
																			if key in text_config_dict:
																							_lowerCamelCase     :			int    =    (
																							    F'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
																							    F'''The value `text_config_dict["{key}"]` will be used instead.'''
																							)
																			# If inferred from default argument values (just to be super careful)
																			else:
																							_lowerCamelCase     :			Optional[int]    =    (
																							    F'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
																							    F'''value `text_config["{key}"]` will be overriden.'''
																							)
																			logger.warning(lowercase  )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
											text_config.update(_text_config_dict  )

							if vision_config_dict is not None:
											if vision_config is None:
															_lowerCamelCase     :			List[str]    =    {}

											# This is the complete result when using `vision_config_dict`.
											_lowerCamelCase     :			List[Any]    =    AltCLIPVisionConfig(**lowercase  ).to_dict()
											# convert keys to string instead of integer
											if "id2label" in _vision_config_dict:
															_lowerCamelCase     :			int    =    {
															    str(lowercase  ): value for key, value in _vision_config_dict['id2label'].items()
															}

											# Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
											for key, value in _vision_config_dict.items():
															if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
																			# If specified in `vision_config_dict`
																			if key in vision_config_dict:
																							_lowerCamelCase     :			Optional[Any]    =    (
																							    F'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
																							    F'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
																							)
																			# If inferred from default argument values (just to be super careful)
																			else:
																							_lowerCamelCase     :			Optional[int]    =    (
																							    F'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
																							    F'''The value `vision_config["{key}"]` will be overriden.'''
																							)
																			logger.warning(lowercase  )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
											vision_config.update(_vision_config_dict  )

							if text_config is None:
											_lowerCamelCase     :			Dict    =    {}
											logger.info('`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.'  )

							if vision_config is None:
											_lowerCamelCase     :			List[Any]    =    {}
											logger.info('`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.'  )

							_lowerCamelCase     :			List[str]    =    AltCLIPTextConfig(**lowercase  )
							_lowerCamelCase     :			Optional[int]    =    AltCLIPVisionConfig(**lowercase  )

							_lowerCamelCase     :			Optional[Any]    =    projection_dim
							_lowerCamelCase     :			List[Any]    =    logit_scale_init_value
							_lowerCamelCase     :			List[str]    =    1.0
			@classmethod
			def 					A_    (  cls  ,      lowercase  ,      lowercase  ,      **lowercase  ):
							return cls(text_config=text_config.to_dict()  ,      vision_config=vision_config.to_dict()  ,      **lowercase  )






			def 					A_    (  self  ):
							_lowerCamelCase     :			str    =    copy.deepcopy(self.__dict__  )
							_lowerCamelCase     :			Tuple    =    self.text_config.to_dict()
							_lowerCamelCase     :			List[Any]    =    self.vision_config.to_dict()
							_lowerCamelCase     :			List[str]    =    self.__class__.model_type
							return output






'''simple docstring'''




import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast







@require_vision
class   A_	(  unittest.TestCase			):




 def      lowercase	(					self       :    int    ):
      _UpperCAmelCase     							=   tempfile.mkdtemp()

      _UpperCAmelCase     							=   BlipImageProcessor()
      _UpperCAmelCase     							=   BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel"    )

      _UpperCAmelCase     							=   BlipProcessor(snake_case_	,			snake_case_    )

      processor.save_pretrained(self.tmpdirname    )




 def      lowercase	(					self       :    Tuple	,			**snake_case_       :    int    ):
      return AutoProcessor.from_pretrained(self.tmpdirname	,			**snake_case_    ).tokenizer




 def      lowercase	(					self       :    Dict	,			**snake_case_       :    Any    ):
      return AutoProcessor.from_pretrained(self.tmpdirname	,			**snake_case_    ).image_processor




 def      lowercase	(					self       :    int    ):
      shutil.rmtree(self.tmpdirname    )




 def      lowercase	(					self       :    Optional[Any]    ):
      _UpperCAmelCase     							=   [np.random.randint(2_5_5	,			size=(3, 3_0, 4_0_0)	,			dtype=np.uinta    )]

      _UpperCAmelCase     							=   [Image.fromarray(np.moveaxis(snake_case_	,			0	,			-1    )    ) for x in image_inputs]

      return image_inputs




 def      lowercase	(					self       :    int    ):
      _UpperCAmelCase     							=   BlipProcessor(tokenizer=self.get_tokenizer()	,			image_processor=self.get_image_processor()    )
      processor.save_pretrained(self.tmpdirname    )

      _UpperCAmelCase     							=   self.get_tokenizer(bos_token="(BOS)"	,			eos_token="(EOS)"    )
      _UpperCAmelCase     							=   self.get_image_processor(do_normalize=snake_case_	,			padding_value=1.0    )

      _UpperCAmelCase     							=   BlipProcessor.from_pretrained(
          self.tmpdirname	,			bos_token="(BOS)"	,			eos_token="(EOS)"	,			do_normalize=snake_case_	,			padding_value=1.0    )

      self.assertEqual(processor.tokenizer.get_vocab()	,			tokenizer_add_kwargs.get_vocab()    )
      self.assertIsInstance(processor.tokenizer	,			snake_case_    )

      self.assertEqual(processor.image_processor.to_json_string()	,			image_processor_add_kwargs.to_json_string()    )
      self.assertIsInstance(processor.image_processor	,			snake_case_    )




 def      lowercase	(					self       :    Any    ):
      _UpperCAmelCase     							=   self.get_image_processor()
      _UpperCAmelCase     							=   self.get_tokenizer()

      _UpperCAmelCase     							=   BlipProcessor(tokenizer=snake_case_	,			image_processor=snake_case_    )

      _UpperCAmelCase     							=   self.prepare_image_inputs()

      _UpperCAmelCase     							=   image_processor(snake_case_	,			return_tensors="np"    )
      _UpperCAmelCase     							=   processor(images=snake_case_	,			return_tensors="np"    )

      for key in input_feat_extract.keys():
           self.assertAlmostEqual(input_feat_extract[key].sum()	,			input_processor[key].sum()	,			delta=1e-2    )




 def      lowercase	(					self       :    Optional[int]    ):
      _UpperCAmelCase     							=   self.get_image_processor()
      _UpperCAmelCase     							=   self.get_tokenizer()

      _UpperCAmelCase     							=   BlipProcessor(tokenizer=snake_case_	,			image_processor=snake_case_    )

      _UpperCAmelCase     							=   "lower newer"

      _UpperCAmelCase     							=   processor(text=snake_case_    )

      _UpperCAmelCase     							=   tokenizer(snake_case_	,			return_token_type_ids=snake_case_    )

      for key in encoded_tok.keys():
           self.assertListEqual(encoded_tok[key]	,			encoded_processor[key]    )




 def      lowercase	(					self       :    Optional[Any]    ):
      _UpperCAmelCase     							=   self.get_image_processor()
      _UpperCAmelCase     							=   self.get_tokenizer()

      _UpperCAmelCase     							=   BlipProcessor(tokenizer=snake_case_	,			image_processor=snake_case_    )

      _UpperCAmelCase     							=   "lower newer"
      _UpperCAmelCase     							=   self.prepare_image_inputs()

      _UpperCAmelCase     							=   processor(text=snake_case_	,			images=snake_case_    )

      self.assertListEqual(list(inputs.keys()    )	,			["pixel_values", "input_ids", "attention_mask"]    )

      # test if it raises when no input is passed
      with pytest.raises(snake_case_    ):
           processor()




 def      lowercase	(					self       :    Union[str, Any]    ):
      _UpperCAmelCase     							=   self.get_image_processor()
      _UpperCAmelCase     							=   self.get_tokenizer()

      _UpperCAmelCase     							=   BlipProcessor(tokenizer=snake_case_	,			image_processor=snake_case_    )

      _UpperCAmelCase     							=   [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

      _UpperCAmelCase     							=   processor.batch_decode(snake_case_    )
      _UpperCAmelCase     							=   tokenizer.batch_decode(snake_case_    )

      self.assertListEqual(snake_case_	,			snake_case_    )




 def      lowercase	(					self       :    str    ):
      _UpperCAmelCase     							=   self.get_image_processor()
      _UpperCAmelCase     							=   self.get_tokenizer()

      _UpperCAmelCase     							=   BlipProcessor(tokenizer=snake_case_	,			image_processor=snake_case_    )

      _UpperCAmelCase     							=   "lower newer"
      _UpperCAmelCase     							=   self.prepare_image_inputs()

      _UpperCAmelCase     							=   processor(text=snake_case_	,			images=snake_case_    )

      # For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
      self.assertListEqual(list(inputs.keys()    )	,			["pixel_values", "input_ids", "attention_mask"]    )




